package com.example.controller;

import com.example.common.Result;
import com.example.entity.TNotes;
import com.example.service.ReIDService;
import com.example.service.TNotesService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/tNotes")
public class TNotesController {

    @Resource
    private TNotesService tNotesService;
    @Resource
    private ReIDService reIDService;
    /**
     * 新增帖子
     */
    @PostMapping("/add")
    public Result add(@RequestPart("notes") TNotes tNotes, 
                     @RequestPart("file") MultipartFile file) {
        try {
            tNotesService.add(tNotes, file);
            reIDService.Person_ReID_Identification(file, tNotes.getUid().toString());
            return Result.success("上传成功，请耐心等待系统处理");
        } catch (IOException e) {
            return Result.error("图片上传失败：" + e.getMessage());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{nid}")
    public Result deleteById(@PathVariable Long nid) {
        tNotesService.deleteById(nid);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateBykey(@RequestBody TNotes tNotes) {
        tNotesService.updateBykey(tNotes);
        return Result.success();
    }

    /**
     * 根据ID查询帖子详情，寻人照片，用户照片和信息
     */
    @GetMapping("/selectById/{nid}")
    public Result selectById(@PathVariable Long nid) {
        TNotes tNotes = tNotesService.selectByNId(nid);
        return Result.success(tNotes);
    }

    /**
     * 根据Uid查询用户，发布的所有帖子
     */
    @GetMapping("/selectNotesByUid")
    public Result selectNotesByUid(@RequestParam Integer uid){
        List<TNotes> notesList = tNotesService.selectByUid(uid);
        return Result.success(notesList);
    }

    /**
     * 查询所有帖子，寻人照片+描述
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<TNotes> list = tNotesService.selectAll();
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(TNotes tNotes,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<TNotes> page = tNotesService.selectPage(tNotes, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     *条件查询/关键词查询
     * @param keys
     * @return
     */
    @GetMapping("/{keys}")
    public Result findPageByKeys(@PathVariable String keys) {
        List<TNotes> list = tNotesService.findPageByKeys(keys);
        return Result.success(list);
    }

}
